<template>
  <div class="app-container">
    <el-row style="margin-bottom: 10px; width: 50%;">
      <el-button v-if="showBack" class="pan-btn success-btn" @click="back()" style="float: right;" type="success" size="mini">
        返回稳考室
      </el-button>
      <el-button v-else class="pan-btn success-btn" @click="showGet" icon="el-icon-s-promotion" style="float: right;" type="success" size="mini">
        发送并取样
      </el-button>
    </el-row>

    <el-card shadow="hover" class="card" style="width: 50%;">
        <el-form :model="checkList" label-width="80px">
          <el-form-item>
            <span slot="label">品&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;名:</span>
            <el-form :inline="true">
              <el-form-item>
                {{product.productName}}
              </el-form-item>
              <el-form-item label="物料编码:">
                {{product.productGmp}}
              </el-form-item>
              <el-form-item label="货品规格:">
                <span v-html="product.productSpec"></span>
              </el-form-item>
            </el-form>
          </el-form-item>

          <el-form-item label="报告单号:">
            {{ checkList.checkNum }}
          </el-form-item>

          <el-form-item label="考察信息:">
            {{ wkList.stabilityModeName + ' ' + wkList.timeValue + wkList.timeName }}
          </el-form-item>

          <el-form-item label="计划时间:">
            {{ plan.nextTime.timePoint | changeTime }}
          </el-form-item>

          <el-form-item label="计划数量:">
            {{ plan.nextTime.getQuantity + ' ' + plan.nextTime.getUnitName }}
          </el-form-item>

          <el-form-item label="包装规格:" prop="productPackSpec">
            {{ checkList.productPackSpec }}
          </el-form-item>

          <el-form-item label="请检项目:" prop="isAllTest">
            {{ plan.itemNames }}
            <el-button
              style="margin-left: 20px;"
              class="pan-btn info-btn"
              type="info"
              @click="showStandardDrawer"
              size="mini">
              查看项目
            </el-button>
          </el-form-item>

          <el-form-item label="生产车间:">
            {{ wkList.workshopName }}
          </el-form-item>

          <el-form-item>
            <span slot="label">批&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;号:</span>
            {{ checkList.batchNum }}
          </el-form-item>

          <el-form-item label="生产日期:">
            {{ wkList.productionDate | changeTime }}
          </el-form-item>

          <el-form-item label="有效期至:">
            {{ wkList.validityDate | changeTime }}
          </el-form-item>

          <el-form-item label="请验日期:">
            <el-date-picker
              v-model="checkList.registCheckDate"
              align="right"
              type="date"
              placeholder="选择日期"
              style="width: 100%;"
              format="yyyy 年 MM 月 dd 日"
              value-format="yyyy-MM-dd HH:mm:ss"
              size="small">
            </el-date-picker>
          </el-form-item>

          <el-form-item>
            <span slot="label">请&nbsp;&nbsp;验&nbsp;&nbsp;人:</span>
            {{ checkList.registCheckUserName }}
          </el-form-item>

          <el-form-item>
            <span slot="label">备&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;注:</span>
            <el-input v-model="checkList.remark" placeholder="备注" size="small"/>
          </el-form-item>
        </el-form>
      </el-card>

      <el-dialog
        title="取样"
        :visible.sync="getVisible"
        :before-close="closeGet"
        :close-on-click-modal="false"
        width="50%">
        注：此操作会记录在《取样台账记录》和《稳定性考察样品使用记录》
        <el-row>
          <el-col :span="24">
            <el-card>
              <el-form>
                <el-form-item>
                  <el-input v-model="checkList.getQuantity" placeholder="取样量(数值)">
                    <template slot="append">{{ checkList.getUnitName }}</template>
                  </el-input>
                  <el-date-picker
                    v-model="checkList.getDate"
                    align="right"
                    type="date"
                    placeholder="取样日期"
                    :clearable="false"
                    style="width: 100%; margin-top: 10px;"
                    format="yyyy 年 MM 月 dd 日"
                    value-format="yyyy-MM-dd HH:mm:ss">
                  </el-date-picker>
                </el-form-item>
              </el-form>
            </el-card>
          </el-col>
        </el-row>
        <span slot="footer" class="dialog-footer">
          <el-button @click="closeGet">取 消</el-button>
          <el-button class="pan-btn primary-btn" type="primary" :loading="submiting" @click="getOk">确 定</el-button>
      	</span>
      </el-dialog>

      <standard-drawer
        :childDataForm="childDataForm"
        @close="closeStandardDrawer"
        @getStandard="getStandard"
        v-if="standardDrawerVisible" />

      <print
        v-if="printVisible"
        @close="closeInner('printVisible')"
        :childDataForm="childDataForm" />
    </div>
</template>

<script>
  import { mapGetters } from 'vuex'
  import { registCheck } from '@/api/check'
  import { CODE_OK } from '@/common/js/config'
  import { isBlank,prefixZero } from '@/common/js/utils'
  import { isPositiveNumber } from '@/common/js/validate'
  import StandardDrawer from '@/views/modules/base/standard-drawer'
  import Print from '@/views/modules/print'
  import Calc from 'calculatorjs'
  export default {
    name: 'registcheck600',
    components: {
    	StandardDrawer,Print
    },
    data() {
      return {
        plan: {},
        checkList: {
          printCheckVersion: '00'
        },
        wkList: {},
        product: "",
        standard: "",
        allItems: [],
        items: [],
        childDataForm: "",
        standardDrawerVisible: false,
        getVisible: false,
        submiting: false,
        rag: true,
        showBack: false,
        printVisible: false
      }
    },
    created() {
      if (!isBlank(this.$route.params.planId)) {
        this.setRegistInfo()
      }
    },
    computed: {
      ...mapGetters([
         'userInfo'
      ])
    },
    methods: {
      back() {
        window.history.back()
      },
      print() {
        this.childDataForm = JSON.parse(JSON.stringify(this.checkList))
        this.$set(this.childDataForm, 'printClassify', '请验单')
        this.printVisible = true
      },
      closeInner(prop) {
        this[prop] = false
        this.childDataForm = ""
      },
      setRegistInfo() {
        let p = this.$route.params
        this.product = {
          productId: p.productId,
          productGmp: p.productGmp,
          productName: p.productName,
          productSpec: p.productSpec
        }
        this.standard = p.standard
        this.allItems = p.allItems
        if (p.nextTime.isStability == 0) {
          p.now++
        }
        //判断是否最后一次，status 包括增测项
        if (p.times[p.times.length-1].planTimeId == p.nextTime.planTimeId) {
          p.status = 2
        }
        p.nextTime.status = 2
        this.items = p.nextTime.bitmes
        let batchNum = p.batchNum
        let batchNumber = batchNum.replace(/[a-zA-Z]/g, '').replace(' ', '')
        let checkNum = p.productGmp + '-'
         + '@-'
         + (p.stabilityModeName == '加速' ? 'JS' : 'CQ') + '-'
         + prefixZero(JSON.parse(JSON.stringify(p.now)), 2)
        this.checkList = Object.assign({},this.checkList,{
          checkNum: checkNum,
          checkTypeId: 6,
          checkTypeName: '稳定性考察',
          isTest: 0,
          isAllTest: p.isAllTest,
          isMerge: 1,
          status: 1,
          productPackSpec: p.productPackSpec,
          batchNum: batchNum,
          batchNumPrefix: batchNum,
          getQuantity: p.nextTime.getQuantity,
          getUnitId: p.nextTime.getUnitId,
          getUnitName: p.nextTime.getUnitName,
          registCheckDepartmentId: this.userInfo.departmentId,
          registCheckDepartmentName: this.userInfo.departmentName,
          registCheckUserId: this.userInfo.userId,
          registCheckUserName: this.userInfo.name,
          remark: p.envT
        })
        if (isBlank(p.envT)) {
          this.$message.warning('此计划的温度没有设定，请留意')
        }
        this.wkList = Object.assign({},this.wkList,{
          planTimeId: p.nextTime.planTimeId,
          stabilityModeId: p.stabilityModeId,
          stabilityModeName: p.stabilityModeName,
          timeValue: p.nextTime.timeValue,
          timeId: p.nextTime.timeId,
          timeName: p.nextTime.timeName,
          workshopId: p.workshopId,
          workshopName: p.workshopName,
          productionDate: p.productionDate,
          validityDate: p.validityDate
        })
        this.plan = JSON.parse(JSON.stringify(p))
      },
      getOk() {
        if (!isPositiveNumber(this.checkList.getQuantity)) {
          this.$message.error('请输入正数值类型')
          this.checkList.getQuantity = null
        }
        if (isBlank(this.checkList.getDate)) {
          this.$message.error('请选择取样时间')
          return
        }
        let surplus = Calc(this.plan.inventory + '-' + this.checkList.getQuantity)
        if (surplus < 0) {
          this.$message.error('库存量不够了哦~')
          return
        }
        this.plan.inventory = surplus
        this.submiting = true
        this.getVisible = false
        this.submit()
      },
      submit() {
         if (this.formVerify()) {
           const data = {
             rag: this.rag,
             content: 'wk',
             product: this.product,
             // fromStatus: this.fromStatus,
             // checkReturnId: this.checkReturnId,
             plan: this.plan,
             checkList: this.checkList,
             wkList: this.wkList,
             standard: this.standard,
             items: this.items
           }
           registCheck(data).then(res => {
             if (res.code === CODE_OK) {
               this.checkList = res.list
               this.print()
               this.showBack = true
               this.$message({
                 message: '提交成功',
                 type: 'success',
                 duration: 1000
               })
             }else{
               this.$message.error(res.msg)
             }
             this.submiting = false
           })
         }
      },
      formVerify() {
        if (isBlank(this.checkList.getDate)) {
          this.$message.error("取样时间不能为空")
          return false
        }
        return true
      },
      showGet() {
        if (isBlank(this.checkList.registCheckDate)) {
          this.$message.error("请先填写请验时间")
          return
        }
        this.checkList.getDate = JSON.parse(JSON.stringify(this.checkList.registCheckDate))
        this.getVisible = true
      },
      closeGet() {
        this.getVisible = false
      },
      showStandardDrawer() {
        this.childDataForm = {
          product: this.product,
          standard: this.standard,
          allItems: this.allItems,
          checkedItems: this.items
        }
        this.standardDrawerVisible = !this.standardDrawerVisible
      },
      closeStandardDrawer() {
        this.childDataForm = ''
        this.standardDrawerVisible = !this.standardDrawerVisible
      },
      getStandard(standardObj) {
        this.$message.warning("无法改变此次请验的检验项目,若想修改,请、请于稳考计划中修改")
      }
    }
  }
</script>

<style scoped>
  .el-form-item {
    margin-bottom: 8px !important;
  }
  .el-form-item__content .el-input-group {
    vertical-align: inherit !important;
  }
</style>
